<template>
    <div class="user-management">
        <div style="display: flex;justify-content: flex-end;" fit>
            <!-- 搜索用户 -->
            <div style="display: flex;">
                <el-input v-model="filterKeyword" placeholder="请输入用户名"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
        </div>
        <!-- 列表信息 -->
        <el-table :data="filteredUsers" style="width: 100%;margin-top: 20px;" fit border>
            <el-table-column width="180" label="姓名" align="center">
                <template slot-scope="scope">
                    <p style="margin-left: 10px" v-if="scope.row.username != null">{{ scope.row.username }}</p>
                    <p v-if="scope.row.username == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="头像" align="center">
                <template slot-scope="scope">
                    <img v-if="scope.row.image != null" style="width:180px" :src="scope.row.image" />
                    <p v-if="scope.row.image == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="简介" align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.brief != null">{{ scope.row.brief }}</p>
                    <p v-if="scope.row.brief == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="性别" align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.sex != null && scope.row.sex == 1">女</p>
                    <p v-if="scope.row.sex != null && scope.row.sex == 0">男</p>
                    <p v-if="scope.row.sex == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="生日" align="center">
                <template slot-scope="scope">
                    <p v-if="scope.row.birthady !== null">{{ new Date(scope.row.birthady).getFullYear() + "-"
                        + (new Date(scope.row.birthady).getMonth() + 1) + "-"
                        + new Date(scope.row.birthady).getDate()
                    }}</p>
                    <p v-if="scope.row.birthady == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="背景图" align="center">
                <template slot-scope="scope">
                    <img v-if="scope.row.background != null" style="width: 180px;" :src="scope.row.background"
                        alt="图片信息错误" />
                    <p v-if="scope.row.background == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="关注、粉丝、喜欢" align="center">
                <template slot-scope="scope">
                    <el-popover v-if="scope.row.acctention != null || scope.row.fan != null || scope.row.like != null"
                        trigger="hover" placement="top">
                        <p>我的关注: {{ scope.row.attention }}</p>
                        <p>我的粉丝：{{ scope.row.fan }}</p>
                        <p>我的喜欢：{{ scope.row.like }}</p>
                        <div slot="reference" class="name-wrapper">
                            <el-tag size="medium">{{ scope.row.attention }}</el-tag>
                            <el-tag size="medium">{{ scope.row.fan }}</el-tag>
                            <el-tag size="medium">{{ scope.row.like }}</el-tag>
                        </div>
                    </el-popover>
                    <el-popover v-if="scope.row.attention == null && scope.row.fan == null && scope.row.like == null">
                        <div slot="reference" class="name-wrapper">
                            <el-tag size="medium">个人信息未完善</el-tag>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column width="180" label="电话" align="center">
                <template slot-scope="scope">
                    <p>{{ scope.row.phone }}</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="密码" align="center">
                <template slot-scope="scope">
                    <p>{{ scope.row.cipher }}</p>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="handlePageSizeChange" @current-change="handleCurrentPageChange"
            :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="total"
            layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
</template>
<script>
export default {
    data() {
        return {
            users: [],// 获取的数据
            filteredUsers: [],// 获取的数据
            filterKeyword: '',// 查找输入框的内容
            currentPage: 1, // 当前页
            pageSize: 10, // 当前页的条数
            total: 0 // 总条数
        }
    },
    methods: {
        // 获取用户信息
        filterUsers() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            this.$http
                .get('/getuser')
                .then(res => {
                    this.users = res.data;
                    this.filteredUsers = res.data.slice(start, end)
                    this.total = res.data.length
                })
        },
        // 搜索用户
        search() {
            const keyword = this.filterKeyword.trim().toLowerCase()
            if (keyword == '') {
                this.$message.error("请输入用户名")
            } else {
                const start = (this.currentPage - 1) * this.pageSize;
                const end = start + this.pageSize;
                this.$http
                    .post('/seluser', { username: keyword })
                    .then(res => {
                        if (res.data == undefined) {
                            this.$message.error(res.status)
                        } else {
                            this.filteredUsers = res.data.slice(start, end)
                            this.total = res.data.length
                            this.$message.success(res.status)
                        }

                    })
            }

        },
        // 切换每页条数
        handlePageSizeChange(size) {
            this.pageSize = size
            this.currentPage = 1
            this.filterUsers()
        },
        // 切换页码
        handleCurrentPageChange(page) {
            this.currentPage = page
            this.filterUsers()
        },
    },
    mounted() {
        this.filterUsers()
    },
}
</script>
